<template>
  <div class="data-table-container">
    <el-table :data="tableData" v-loading="loading" border>
      <el-table-column prop="seckillName" label="活动名称" min-width="120" />
      <el-table-column prop="state" label="状态" width="120">
        <template #default="scope">
          <el-tag :type="formatStatusType(scope.row.state)">
            {{ formatStatus(scope.row.state) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="活动内容" />
      <el-table-column prop="signTime" label="报名时间" width="180" />
      <el-table-column prop="time" label="起止时间" width="180" />
      <el-table-column prop="shops" label="商家数" width="80" />
      <el-table-column prop="products" label="商品数量" width="100" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">详情</el-button>
          <el-button type="primary" link @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, watch } from 'vue'
  import { ElMessage, ElMessageBox } from 'element-plus'

  interface TableData {
    seckillId: number
    seckillName: string
    content: string
    signTime: string
    time: string
    shops: number
    products: number
    state: number
  }

  interface Props {
    tableData: TableData[]
    loading: boolean
    total: number
  }

  const props = withDefaults(defineProps<Props>(), {
    tableData: () => [],
    loading: false,
    total: 0
  })

  const emit = defineEmits<{
    (e: 'view', row: TableData): void
    (e: 'edit', row: TableData): void
    (e: 'delete', row: TableData): void
    (e: 'size-change', val: number): void
    (e: 'current-change', val: number): void
  }>()

  const currentPage = ref(1)
  const pageSize = ref(10)

  // 格式化状态显示
  const formatStatus = (status: number) => {
    const statusMap: Record<number, string> = {
      0: '报名未开始',
      1: '报名进行中',
      2: '活动待开始',
      3: '活动进行中',
      4: '活动已结束'
    }
    return statusMap[status] || '未知状态'
  }

  // 格式化状态标签类型
  const formatStatusType = (status: number) => {
    const typeMap: Record<number, 'info' | 'success' | 'warning' | 'danger' | ''> = {
      0: 'info', // 报名未开始 - 灰色
      1: 'success', // 报名进行中 - 绿色
      2: 'warning', // 活动待开始 - 橙色
      3: 'success', // 活动进行中 - 绿色
      4: 'danger' // 活动已结束 - 红色
    }
    return typeMap[status] || 'info'
  }

  // 查看详情
  const handleView = (row: TableData) => {
    emit('view', row)
  }

  // 编辑
  const handleEdit = (row: TableData) => {
    emit('edit', row)
  }

  // 删除
  const handleDelete = (row: TableData) => {
    ElMessageBox.confirm('确定要删除该秒杀活动吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
      .then(() => {
        emit('delete', row)
      })
      .catch(() => {
        // 取消操作
      })
  }

  // 处理分页大小变化
  const handleSizeChange = (val: number) => {
    pageSize.value = val
    emit('size-change', val)
  }

  // 处理当前页变化
  const handleCurrentChange = (val: number) => {
    currentPage.value = val
    emit('current-change', val)
  }

  // 监听总数据变化，重置当前页
  watch(
    () => props.total,
    (newVal) => {
      if (newVal === 0) {
        currentPage.value = 1
      }
    }
  )
</script>

<style scoped lang="scss">
  .data-table-container {
    .pagination-container {
      margin-top: 20px;
      display: flex;
      justify-content: flex-end;
    }
  }
</style>
